<template>
  <div>
    <!-- 三级分类 -->
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <el-form :inline="true" :model="attrForm">
        <el-form-item label="一级分类">
          <el-select
            v-model="attrForm.c1Id"
            placeholder="请选择"
            @change="handlerC1"
          >
            <el-option
              :label="c1.name"
              :value="c1.id"
              v-for="(c1, index) in c1Arr"
              :key="c1.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select
            v-model="attrForm.c2Id"
            placeholder="请选择"
            @change="handlerC2"
          >
            <el-option
              :label="c2.name"
              :value="c2.id"
              v-for="(c2, index) in c2Arr"
              :key="c2.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="三级分类">
          <el-select v-model="attrForm.c3Id" placeholder="请选择"  @change="handlerC3">
            <el-option
              :label="c3.name"
              :value="c3.id"
              v-for="(c3, index) in c3Arr"
              :key="c3.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name:"Category",
  props:["fetchCategoryId"],
  data() {
    return {
      msg: "",
      c1Arr: [], //一级分类的数据
      c2Arr: [], //二级分类的数据
      c3Arr: [], //三级分类的数据
      attrForm: {
        c1Id: "",
        c2Id: "",
        c3Id: "",
      },
    };
  },
  mounted() {
    //获取一级分类数据
    this.getV1List();
  },
  methods: {
    async getV1List() {
      // 发请求 获取数据
      let res = await this.$http.attr.reqC1();
      // console.log(res)
      if (res.code == 200) {
        this.c1Arr = res.data;
      }
    },
    // 一级分类回调
    async handlerC1() {
      //给父亲传入id值
      this.fetchCategoryId(this.attrForm.c1Id,1);

      // 清空二级 三级数据
      this.attrForm.c2Id = "";
      this.attrForm.c3Id = "";

      //清空数组中的数据
      this.c2Arr = [];
      this.c3Arr = []; 

      // 整理参数
      const { c1Id } = this.attrForm;

      // 发请求 获取二级分类的数据
      let res = await this.$http.attr.reqC2(c1Id);
      // console.log(res)
      if (res.code == 200) {
        this.c2Arr = res.data;
      }
    },
    // 二级分类回调
    async handlerC2() {
      //给父亲传入id值
      this.fetchCategoryId(this.attrForm.c2Id,2);

      // 清空 三级数据
      this.attrForm.c3Id = "";

      //清空数组中的数据
      this.c3Arr = []; 


      // 整理参数
      const { c2Id } = this.attrForm;
      // 发请求 获取二级分类的数据
      let res = await this.$http.attr.reqC3(c2Id);
      //   console.log(res);
      if (res.code == 200) {
        this.c3Arr = res.data;
      }
    },
     // 三级分类回调
     handlerC3() {
      //给父亲传入id值
      this.fetchCategoryId(this.attrForm.c3Id,3);
    }


  },
};
</script>

</script>

<style>
</style>